/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.home;

import it.francescogabbrielli.android.solidalapp.AbstractMapFragment;
import it.francescogabbrielli.android.solidalapp.DataEvent;
import it.francescogabbrielli.android.solidalapp.MapUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Marker;

/**
 * 
 */
public class RequestMapFragment extends AbstractMapFragment<HomeFragmentListener> {
			
	@Override
	public void onDataChanged(DataEvent event) {
		
		if(map==null || !isAdded())
			return;
		
		super.onDataChanged(event);
		
		Marker m = getMarker();
		if(m==null)
			return;
				
		DataKey key = event.getKey();
		
		switch(key) {
		
		// The address text has changed by reverse lookup
		case ADDRESS_TEXT:
			if (app.getData(DataKey.ADDRESS_TEXT_MANUAL)==null)
				updateMarker(m);
			break;
			
		// The request has been sent or is sending
		case REQUEST:
			updateMarker(m);
			break;
		default:
		}
		
//		if ("".equals(m.getTitle()))
//			System.out.println();
//		Log.i(SolidalApp.TAG, "TITLE="+m.getSnippet()+"; EVENT="+event+"; VALUE="+app.getData(key));
		
//		if(m.isInfoWindowShown())
//			m.showInfoWindow();
	}
	
	@Override
	public void onInfoWindowClick(Marker marker) {
		if(getString(R.string.map_marker_click).equals(marker.getSnippet())) {
			marker.hideInfoWindow();
			marker.setSnippet("");
			interactionListener.send(marker.getPosition());
		} else if(getString(R.string.map_marker_accepted).equals(marker.getSnippet())) {
			interactionListener.openFile();
		}
	}
	
	private void updateMarker(Marker m) {
		
		Request request = (Request) app.getData(DataKey.REQUEST);
		
		String snippet = "";
		String title = (String) app.getData(DataKey.ADDRESS_TEXT);
		if(interactionListener!=null && interactionListener.getRequest()!=null) {
			snippet = getString(R.string.map_marker_click);
			title = interactionListener.getRequest().getDetails();
		}
		if (request!=null)
			title = request.getDetails();
		
		// request has been reset
		if (request==null || (!request.getAccepted() && request.getExpired())) {

			MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_RED);
			m.setDraggable(true);
			
		// request is just sending
		} else if (request.getId()==null) {
				
			m.setDraggable(false);
			MapUtils.startBlinkIcon(getActivity(), m, 900, Long.MAX_VALUE, new float[] {
				BitmapDescriptorFactory.HUE_GREEN, 
				BitmapDescriptorFactory.HUE_RED}
			);
			snippet= getString(R.string.map_marker_sending);
			
		// request has been sent (has the key...)
		} else if (!request.getAccepted()) {
			
			MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_GREEN);
			snippet = getString(R.string.map_marker_sent);
			
		// request sent and accepted
		} else {
			
			MapUtils.stopBlinkIcon(m, BitmapDescriptorFactory.HUE_AZURE);
			snippet = getString(R.string.map_marker_accepted);
			
		}

		m.setTitle(title);
		m.setSnippet(snippet);
		if (mapShowInfo)
			m.showInfoWindow();
		
	}
	
}
